package org.example.managementsystem.repository;
import org.example.managementsystem.model.Production_Submission_Detail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Objects;
@Repository
public class Production_Submission_DetailDao {
    private  final JdbcTemplate jdbcTemplate;
    @Autowired

    public Production_Submission_DetailDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //添加计划
    public int insertProduction_Submission_Detail(Production_Submission_Detail production_submission_detail) {
        String sql = "INSERT INTO production_submission_detail ( submission_id, product_code, product_quantity) " +
                "VALUES (?, ?, ?)";

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(connection -> {
            PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

            ps.setInt(1,production_submission_detail.getSubmission_id());
            ps.setString(2,production_submission_detail.getProduct_code());
            ps.setInt(3,production_submission_detail.getProduct_quantity());
            return ps;
        }, keyHolder);
        return Objects.requireNonNull(keyHolder.getKey()).intValue();
    }
    public int updateProduction_Submission_Detail(Production_Submission_Detail production_submission_detail) {
        String sql = "UPDATE production_submission_detail SET  submission_id = ?, product_code = ?, product_quantity = ?" +
                "WHERE detail_id = ?";

        return jdbcTemplate.update(sql,
                production_submission_detail.getSubmission_id(),
                production_submission_detail.getProduct_code(),
                production_submission_detail.getProduct_quantity(),
                production_submission_detail.getDetail_id());
    }
    public Production_Submission_Detail getProduction_Submission_DetailById(int detail_id) {
        String sql = "SELECT * FROM production_submission_detail WHERE detail_id = ?";
        List<Production_Submission_Detail> production_submission_details = jdbcTemplate.query(sql, new Production_Submission_DetailRowMapper(), detail_id);
        return production_submission_details.isEmpty() ? null : production_submission_details.get(0);
    }
    public int deleteProduct_Plan_Detail(int detail_id) {
        String sql = "DELETE FROM production_submission_detail WHERE detail_id = ?";
        return jdbcTemplate.update(sql, detail_id);
    }
    // 根据plan_id查找对应的数据
    public List<Production_Submission_Detail> getProduction_Submission_DetailsBysubmission_id(int submission_id) {
        String sql = "SELECT * FROM production_submission_detail WHERE submission_id = ?";
        return jdbcTemplate.query(sql, new Production_Submission_DetailRowMapper(), submission_id);
    }

    public List<Production_Submission_Detail> getAllProduction_Submission_Details() {
        String sql = "SELECT * FROM production_submission_detail";
        return jdbcTemplate.query(sql, new Production_Submission_DetailRowMapper());
    }


    private static class Production_Submission_DetailRowMapper implements RowMapper<Production_Submission_Detail> {
        @Override
        public Production_Submission_Detail mapRow(ResultSet rs, int rowNum) throws SQLException {
            Production_Submission_Detail production_submission_detail = new Production_Submission_Detail();
            production_submission_detail.setSubmission_id(rs.getInt("submission_id"));
            production_submission_detail.setDetail_id(rs.getInt("detail_id"));
            production_submission_detail.setProduct_code(rs.getString("product_code"));
            production_submission_detail.setProduct_quantity(rs.getInt("product_quantity"));

            return production_submission_detail;
        }
    }
}
